
import {
  getRepUplinkDestinationApi,
  addRepUplinkDestinationApi,
  deleteRepUplinkDestinationApi,
  updateRepUplinkDestinationApi
} from '@/services/packetPush.js';
import { message } from 'ant-design-vue';

export default {
  namespaced: true,
  state: {
    addRepUplinkDestinationLoading: false,
    repUplinkDestinationLoading: false,
    repUplinkDestinationList: []
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取列表
    async getRepUplinkDestination({ commit }, params) {
      commit('setState', { key: 'repUplinkDestinationLoading', payload: true });
      const rep = await getRepUplinkDestinationApi(params);
      commit('setState', { key: 'repUplinkDestinationList', payload: rep.data });
      commit('setState', { key: 'repUplinkDestinationLoading', payload: false });
    },
    // 新增
    async addRepUplinkDestination({ commit }, params) {
      commit('setState', { key: 'addRepUplinkDestinationLoading', payload: true });
      try {
        const rep = await addRepUplinkDestinationApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepUplinkDestinationLoading', payload: false });
      }
    },
    // 修改
    async updateRepUplinkDestination({ commit }, params) {
      commit('setState', { key: 'addRepReportParamLabelLoading', payload: true });
      try {
        const rep = await updateRepUplinkDestinationApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepReportTemplateLoading', payload: false });
      }
    },
    // 删除
    async deleteRepUplinkDestination({ commit }, params) {
      try {
        const rep = await deleteRepUplinkDestinationApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    }
  }
};
